package com.lanyam.travel.base.controller;


import com.lanyam.travel.base.entity.vo.UserVo;
import com.lanyam.travel.base.utils.JSONResult;
import com.lanyam.travel.base.entity.DistrUser;
import com.lanyam.travel.base.entity.vo.DistrUserVo;
import com.lanyam.travel.base.service.IDistrUserService;
import com.lanyam.travel.base.utils.JsonMapper;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.math.BigDecimal;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.core.type.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;


@RestController
@RequestMapping("/distrUser")
public class DistrUserController {

    @Autowired
    private IDistrUserService distrUserService;

    /**
     * 分页查询数据
     */
    @GetMapping("/getPage")
    public IPage<DistrUserVo> getPage (String queryMap,
                                                @RequestParam(name="page", defaultValue = "1") Integer page,
                                                @RequestParam(name="rows", defaultValue = "10") Integer rows) {
        Map<String, Object> query = JsonMapper.string2Obj(queryMap,new TypeReference<Map<String, Object>>() {});
        query = query==null?new HashMap<>():query;
        IPage<DistrUserVo> pageInfo = distrUserService.getPageDistrUser(query, page, rows);
        return pageInfo;
    }
    /**
     * 按照团队id分组的分页
     * 分页查询数据
     * 废弃，一个团队的分销商可能太多影响效率，按照团队名查询即可
     */
    @GetMapping("/getPageGroupByTeam")
    public IPage<DistrUserVo> getPageGroupByTeam (String queryMap,
                                       @RequestParam(name="page", defaultValue = "1") Integer page,
                                       @RequestParam(name="rows", defaultValue = "10") Integer rows) {
        Map<String, Object> query = JsonMapper.string2Obj(queryMap,new TypeReference<Map<String, Object>>() {});
        IPage<DistrUserVo> pageInfo = distrUserService.getPageGroupByTeam(query, page, rows);
        return pageInfo;
    }

    /**
     * 通过邀请码成为分销员
     */
    @PostMapping("/add")
    public JSONResult add (@RequestBody DistrUserVo distrUser) {
        JSONResult result = null;
        try{
            distrUserService.addDistrUser(distrUser);
            result = JSONResult.ok();
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }

    /**
     * 管理员添加一级分销用户/ 业务员
     */
    @PostMapping("/addDistr")
    public JSONResult addDistr (DistrUser user) {
        JSONResult result = null;
        try{
            distrUserService.addDistrUserByAdmin(user);
            result = JSONResult.ok();
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }

    /**
     * 查询当前登录分销用户信息，微信端
     * @param userId
     * @return
     */
    @RequestMapping(value="/getDistrUserInfo", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public JSONResult getDistrUserInfo (String userId) {
        DistrUserVo vo = distrUserService.getDistrUserByUserId(userId);
        return JSONResult.ok(vo);
    }

    /**
     * 编辑数据
     */
    @PostMapping("/update")
    public JSONResult update (@RequestBody DistrUser distrUser) {
        JSONResult result = null;
        try{
            distrUserService.updateDistrUser(distrUser);
            result = JSONResult.ok();
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }

    /**
     * 删除数据
     */
    @PostMapping("/delById")
    public JSONResult delById (String id) {
        JSONResult result = null;
        try {
            distrUserService.delById(id);
            result = JSONResult.ok();
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }

    /**
     * 根据当前登录用户，获取相应的子级列表
     */
    @GetMapping("/getListByFather")
    public IPage<DistrUserVo> getListByFather (String queryMap,
                                       @RequestParam(name="page", defaultValue = "1") Integer page,
                                       @RequestParam(name="rows", defaultValue = "10") Integer rows) {
        Map<String, Object> query = JsonMapper.string2Obj(queryMap,new TypeReference<Map<String, Object>>() {});
        query = query==null?new HashMap<>():query;
        IPage<DistrUserVo> pageList = null;
        try {
            pageList = distrUserService.getListByFatherUserId(query, page, rows);
        }  catch (Exception e) {
            e.printStackTrace();
        }
        return pageList;
    }

    /**
     * 获取所有分销员
     * @return
     */
    @GetMapping("/getAllDistrUser")
    public JSONResult getAllDistrUser()
    {
    	List<DistrUser> allDistrUser = distrUserService.getAllDistrUser();
    	return JSONResult.ok(allDistrUser);
    }

    /**
     * 申请成为分销员
     * @param distrUser
     * @return
     */
    @PostMapping(value="/applyDistr")
    public JSONResult applyDistr(@RequestBody DistrUserVo distrUser) {
        JSONResult result = null;
        try {
            distrUserService.supplyDistr(distrUser);
            result = JSONResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            result.errorMsg(e.getMessage());
        }
        return result;
    }

    /**
     * 分页查询数据
     */
    @GetMapping("/getUserListAddDistrUser")
    public IPage<UserVo> getUserListAddDistrUser (String queryMap,
                                                  @RequestParam(name="page", defaultValue = "1") Integer page,
                                                  @RequestParam(name="rows", defaultValue = "10") Integer rows) {
        Map<String, Object> query = JsonMapper.string2Obj(queryMap,new TypeReference<Map<String, Object>>() {});
        query = query==null?new HashMap<>():query;
        IPage<UserVo> pageInfo = distrUserService.getUserListAddDistrUser(query, page, rows);
        return pageInfo;
    }
}